if [ $# -eq 0 ];
then
    echo "usage: $0 SERVICE(服务名) PUBLISH(发布地址) NUM(服务个数)"
    exit
fi
function log(){
        echo "$@"
        echo "$@" >> b.log
}
#加载配置文件
declare -A map
function file2Map(){
        mapfile mapList < $1
        for line in ${mapList[@]}
        do
                arr=(${line//=/ })
                map[${arr[0]}]=${arr[1]}
        done
}
file2Map "conf/IP2User.conf"
file2Map "conf/Service2Idx.conf"

CURPWD=$PWD
SERVICE=$1
PUBLISH=$2
#用户名在IP2User.conf中配置
USERNAME=${map[$PUBLISH]}
#服务索引Service2Idx.conf中配置
IDX=${map[$SERVICE]}
NUM=`expr $3 - 1`
NACOS_SERVER=10.35.36.205
VERSION=`cd huantv_ott_zero && git describe`
HUB_HOST=10.35.40.29
DOCKER_HUB=$HUB_HOST:5000
DOCKER_OUTER_HUB=159.75.238.142:5000
IMAGE_NAME=$SERVICE:$VERSION
log "发布$SERVICE到docker hub"
log "当前版本$VERSION"
log "发布到IP:$PUBLISH"
#拷贝到docker hub
IMAGENUM=`docker images|grep "$DOCKER_HUB/$SERVICE"|grep "$VERSION"|grep -v grep|grep -v pred |wc -l | xargs echo`
log "当前版本镜像总数:$IMAGENUM"
if [ $IMAGENUM -eq '0' ]; then
        cd "huantv_ott_zero/jeecg-boot/huantv-cloud-module/huantv-cloud-$SERVICE" && docker build -t $IMAGE_NAME .
        cd $CURPWD
        log "docker tag $IMAGE_NAME $DOCKER_HUB/$IMAGE_NAME"
        docker tag $IMAGE_NAME $DOCKER_HUB/$IMAGE_NAME
        log "docker push $DOCKER_HUB/$IMAGE_NAME"
        docker push $DOCKER_HUB/$IMAGE_NAME
fi
SHELL_FILENAME="$SERVICE"_publish.sh
log "$SHELL_FILENAME"
#4个服务
for((ITEM=0;ITEM<=$NUM;ITEM++))
do
        log "停止服务"
                #nacos下服务
        PORT=9$ITEM$IDX
        XXJPORT=1$PORT
curl --location --request PUT 'http://'${NACOS_SERVER}':8848/nacos/v1/ns/instance' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'serviceName=huantv-'${SERVICE}'' \
--data-urlencode 'ip='${PUBLISH}'' \
--data-urlencode 'port='${PORT}'' \
--data-urlencode 'metadata=preserved.register.source=SPRING_CLOUD' \
--data-urlencode 'enabled=false'
log "下线服务$PUBLISH:$PORT"
        sleep 1
        log -e "#自动生成脚本$SERVICE$ITEM \n\
        if [ "$HUB_HOST" != "$PUBLISH" ]; then #不是本机
                sudo su - \n\
        fi
        IMAGENUM=\`docker images|grep "$DOCKER_HUB/$SERVICE"|grep "$VERSION"|grep -v grep |wc -l | xargs echo\` \n\
        if [ \$IMAGENUM -eq '0' ]; then \n\
                docker pull $DOCKER_HUB/$IMAGE_NAME \n\
        fi \n\
        docker stop $SERVICE$ITEM \n\
        docker rm $SERVICE$ITEM  \n\
        if [ "$HUB_HOST" != "$PUBLISH" ]; then #不是本机才清理
                docker images|grep "$DOCKER_HUB/$SERVICE"|grep -v "$VERSION"|awk '{print \$3}'|xargs -r docker rmi -f \n\
        fi
        docker run -e TZ=\"Asia/Shanghai\" --name $SERVICE$ITEM -v /bak/logs/$SERVICE$ITEM:/logs --env PARAMS=\"-Dspring.profiles.active=prod -Dserver.port=$PORT -Dxxljob.port=$XXJPORT\" --network host -d $DOCKER_HUB/$IMAGE_NAME \n\
        if [ "$HUB_HOST" != "$PUBLISH" ]; then #不是本机
                exit \n\
        fi" > $SHELL_FILENAME
        log "ssh $USERNAME@$PUBLISH < $SHELL_FILENAME"
        if [ "$HUB_HOST" = "$PUBLISH" ]; then
                sh $SHELL_FILENAME #本机执行
        else
                ssh -Tq $USERNAME@$PUBLISH < $SHELL_FILENAME
        fi
log "等待服务启动完成"
        arry=("\\" "|" "/" "-")
        TIMES=0
        while true #等待服务起来
        do
                PROCESSNUM=`curl -s --location --request GET 'http://'${NACOS_SERVER}':8848/nacos/v1/ns/catalog/instances?serviceName=huantv-'${SERVICE}'&clusterName=DEFAULT&groupName=DEFAULT_GROUP&pageSize=100&pageNo=1&namespaceId=' |\
jq .list[] | jq -r '(.ip|tostring)+":"+(.port|tostring)+":"+(.enabled|tostring)+":"+(.healthy|tostring)' |\
                grep "$PUBLISH:$PORT:true:true" | wc -l | xargs echo`
                if [ $PROCESSNUM -eq '0' ]; then
                        let TIMES++
                        let index=TIMES%4
                        printf "服务数：$PROCESSNUM 等待 %c\r" "${arry[$index]}"
                        sleep 1
                else
                        log "$PUBLISH:$PORT已上线"
                        log "部署下一个"
                        break
                fi
        done

done

log "$SERVICE 发布完成"